import asyncio
from asyncio import CancelledError
from util import delay
from PyCmpltrtok.common import sep


def main():
    
    loop = asyncio.get_event_loop()
    
    # long_task = asyncio.create_task(delay(10, 'main_delay', 0.2))
    # RuntimeError: no running event loop
    # sys:1: RuntimeWarning: coroutine 'delay' was never awaited

    long_task = loop.create_task(delay(5, 'main_delay', 0.2))

    try:
        loop.run_until_complete(long_task)    
    except KeyboardInterrupt as ex:
        sep('Exception')
        print(f'KeyboardInterrupt: |{ex}|')
        long_task.cancel()
        print(f'long_task cancelled: {long_task.cancelled()}')
    finally:
        sep('Close loop')
        print(f'long_task cancelled: {long_task.cancelled()}')
        loop.close()
 

if '__main__' == __name__:
    main()
